package com.zd.kids.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.hyy.libray.presenter.IBasePresenter;
import com.hyy.libray.ui.activity.BaseActivity;
import com.zd.kids.DataBean.ChecBookCodeDataBean;
import com.zd.kids.R;
import com.zd.kids.common.ToastUtils;
import com.zd.kids.net.CustomObserver;
import com.zd.kids.net.HttpMethod;
import com.zd.kids.ui.view.VerificationCodeInput;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class InputBookCodeActivity extends BaseActivity {


    public static final String INPUT_CODE_KEY = "INPUT_CODE_KEY";

    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.view_code_input)
    VerificationCodeInput viewCodeInput;

    /**
     * 获取布局文件地址
     */
    @Override
    public int getLayoutResId() {
        return R.layout.activity_input_book_code;
    }

    /**
     * 是否是输入，书籍验证码
     */
    private String mInputCodeKey;


    /**
     * 初始化布局
     */
    @Override
    public void initView() {
        initTitle(getString(R.string.use_qm_app));

        mInputCodeKey = getIntent().getStringExtra(INPUT_CODE_KEY);

        viewCodeInput.setOnCompleteListener(new VerificationCodeInput.Listener() {
            @Override
            public void onComplete(String content, boolean isComplete) {
                if (isComplete) {
                    tvConfirm.setBackgroundResource(R.drawable.shape_bt_bg);
                } else {
                    tvConfirm.setBackgroundResource(R.drawable.shape_btgray_bg);
                }
            }
        });
    }

    /**
     * 获取Presenter，不用MVP，可以为空
     *
     * @return
     */
    @Override
    protected IBasePresenter onBasePresenter() {
        return null;
    }


    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        KeyboardUtils.hideSoftInput(this);
        String code = viewCodeInput.getContent();

        if (!TextUtils.isEmpty(code) && code.length() == 7) {

            if (!TextUtils.isEmpty(mInputCodeKey)) {
                checkBookCode(code);
            } else {
                registcheckBookCode(code);
            }


        } else {
            ToastUtils.getIntence().makeTextShow(getString(R.string.input_succ_code_tips));
        }
    }


    private void registcheckBookCode(String inpuCode) {
        showLoading();
        HttpMethod.getIntence().registcheckBookCode(inpuCode, new CustomObserver<ChecBookCodeDataBean>(this) {
            @Override
            public void onSuccess(ChecBookCodeDataBean checBookCodeDataBean) {
                disLoading();
                goToRegist(inpuCode + "");
            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }


    private void checkBookCode(String inpuCode) {
        showLoading();
        HttpMethod.getIntence().checkBookCode(inpuCode, new CustomObserver<ChecBookCodeDataBean>(this) {
            @Override
            public void onSuccess(ChecBookCodeDataBean checBookCodeDataBean) {
                disLoading();
                ToastUtils.getIntence().makeTextShow(getString(R.string.check_success_tips));
                finish();
            }

            @Override
            public void onError(String errorCode) {
                disLoading();
            }
        });
    }


    private void goToRegist(String proId) {

        if (TextUtils.isEmpty(mInputCodeKey)) {
            Intent intent = new Intent(this, SettingKidsActivity.class);
            intent.putExtra(SettingKidsActivity.BOOK_CODE_KEY, proId);
            startActivity(intent);
        }

        finish();
    }
}
